package cn.lili.modules.plus.serviceImpl;

import cn.lili.common.exception.ServiceException;
import cn.lili.common.security.context.UserContext;
import cn.lili.common.security.enums.VipStatusEnum;
import cn.lili.common.utils.StringUtils;
import cn.lili.modules.member.service.VipService;
import cn.lili.modules.plus.service.UrbanVipClientService;
import cn.lili.modules.system.entity.dos.LiVip;
import cn.lili.modules.system.entity.dto.VipPageDto;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.Instant;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Objects;

@Service
@Slf4j
public class UrbanVipClientServiceImpl implements UrbanVipClientService {
    private Integer OVERTIME=2;
    private Integer NORMAL=1;

    @Autowired
    private VipService vipService;

    @Override
    public Page<LiVip> getVipList(VipPageDto dto) {
        log.info("======getVipList======{},{}", dto.getLoginId(), dto.getExtendId());

        /**
         * 城市站点只能查通过自家激活码开通的plus会员
         */
        LambdaQueryWrapper<LiVip>wrapper=new LambdaQueryWrapper<>();
        wrapper.like(StringUtils.isNotEmpty(dto.getUserName()),LiVip::getUserName,dto.getUserName())
                .like(StringUtils.isNotEmpty(dto.getUserId()),LiVip::getUserId,dto.getUserId())
                        .eq(Objects.nonNull(dto.getVipStatus()),LiVip::getVipStatus,dto.getVipStatus());
        wrapper.and(tmp -> tmp.likeRight(LiVip::getActiveCode, dto.getExtendId()).or().likeRight(LiVip::getActiveCode, dto.getLoginId()));
        return vipService.page(new Page<>(dto.getPageNumber(), dto.getPageSize()), wrapper);
    }

    @Override
    public void vipFreeze(String id) {
        //查询会员，如果被冻结了，则不更新
        LiVip liVip = vipService.getById(id);
        if (Objects.equals(liVip.getVipStatus(),VipStatusEnum.FREEZE.getStatus())||Objects.equals(liVip.getVipStatus(),VipStatusEnum.URBAN_FREEZE.getStatus()))
        {
            throw new ServiceException("该用户已被冻结");
        }
        UpdateWrapper<LiVip>wrapper=new UpdateWrapper<>();
        wrapper.eq("id",id)
                .set("vip_status",VipStatusEnum.URBAN_FREEZE.getStatus());
        vipService.update(wrapper);
    }

    @Override
    public void vipUnfreeze(String id) {
        /**
         * 先查出该plus会员的end_time，根据end_time修改状态
         */
        QueryWrapper<LiVip> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("id",id);
        LiVip liVip = vipService.getOne(queryWrapper);
        if (!Objects.equals(liVip.getVipStatus(), VipStatusEnum.URBAN_FREEZE.getStatus())){
            throw new ServiceException("无法对该用户解冻");
        }
        int result=liVip.getEndTime().compareTo(Date.from(Instant.now()));

        UpdateWrapper<LiVip>wrapper=new UpdateWrapper<>();
        wrapper.eq("id",id)
                .set(result==NORMAL,"vip_status",NORMAL)
                .set(result<NORMAL,"vip_status",OVERTIME);
        vipService.update(wrapper);
    }

    @Override
    public void CheckVip() {
        //查询vip表，核对endTime，过期则修改状态
        List<LiVip> liVips = vipService.getNormalVip();
        if (liVips.size()==0){
            return;
        }
        List<String> aliVips=new ArrayList<>();
        for (int i = 0; i < liVips.size(); i++) {
            Date time = liVips.get(i).getEndTime();
            int result = time.compareTo(new Date());
            if (result<1){
                liVips.get(i).setVipStatus(VipStatusEnum.EXPIRED.getStatus());
                aliVips.add(liVips.get(i).getId());
            }
        }
        LambdaUpdateWrapper<LiVip> wrapper=new LambdaUpdateWrapper<>();
        wrapper.in(LiVip::getId,aliVips)
                        .set(LiVip::getVipStatus,VipStatusEnum.EXPIRED.getStatus());
        vipService.update(wrapper);
    }

}
